<?php

declare(strict_types=1);

namespace Plugin\Attractions\AttractionManagement\Controller;

use Common\CommonController;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Swagger\Annotation\HyperfServer;
use Plugin\Attractions\AttractionManagement\Service\SpotService;

#[HyperfServer(name: 'http')]
#[Controller('attractions/attraction-management')]
class SpotController extends CommonController
{
    #[Inject]
    private SpotService $service;

    #[GetMapping('listSpots')]
    public function listSpots()
    {
        $data = $this->valid([
            'name' => 'string',
            'status' => 'integer',
            'showType' => 'integer',
            'page' => 'integer|min:1',
            'pageSize' => 'integer:min:1|max:100',
            'typeId' => 'integer',
            'sort' => 'integer'
        ], []);
        return $this->success($this->service->page($data));
    }

    #[PostMapping('addSpot')]
    public function addSpot()
    {
        $data = $this->valid([
            'name' => 'required|string',
            'typeId' => 'integer|required',
            'position' => 'array',
            'position.name' => 'string',
            'position.longitude' => 'numeric',
            'position.latitude' => 'numeric',
            'position.altitude' => 'numeric',
            'introTitle' => 'string',
            'intro' => 'string',
            'attractionId' => 'integer|required',
            'coverId' => 'string',
            'showType' => 'integer',
            'iconId' => 'integer',
            'status' => 'integer',
            'sort' => 'integer',
            'tags' => 'array',
            'extension' => 'array',
        ], []);
        $res = $this->service->create($data);
        return empty($res) ? $this->error() : $this->success($res);
    }

    #[GetMapping('deleteSpot')]
    public function deleteSpot()
    {
        $data = $this->valid(['id' => 'required|regex:/^\d+(,\d+)*$/'], []);
        $ids = explode(',', $data['id']);
        return $this->success($this->service->delete($ids));
    }

    #[PostMapping('updateSpot')]
    public function updateSpot()
    {
        $data = $this->valid([
            'id' => 'required|regex:/^\d+(,\d+)*$/',
            'name' => 'string',
            'typeId' => 'integer',
            'position' => 'array',
            'position.name' => 'string',
            'position.longitude' => 'numeric',
            'position.latitude' => 'numeric',
            'position.altitude' => 'numeric',
            'introTitle' => 'string',
            'intro' => 'string',
            'attractionId' => 'integer',
            'coverId' => 'string',
            'showType' => 'integer',
            'iconId' => 'integer',
            'status' => 'integer',
            'sort' => 'integer',
            'extension' => 'array',
        ], []);
        $ids = explode(',', $data['id']);
        return $this->service->update($ids, $data) ? $this->success() : $this->error();
    }

    #[GetMapping('detailSpot')]
    public function detailSpot()
    {
        $data = $this->valid(['id' => 'required|integer'], []);
        return $this->success($this->service->detail(intval($data['id'])));
    }
}
